package com.xingfudazi.web.interceptor;

import com.xingfudazi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author YuJiaMing
 * date 2024年07月25日 23:16
 */
@Slf4j
public class ImInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("访问到了im拦截器了");
        String requestUri = request.getRequestURI();
        if (!requestUri.startsWith("/im")) {
            return false;
        }

        Long userId = SecurityUtils.getUserId();
        requestUri = requestUri.substring(3);
        if (requestUri.contains("?")) {
            requestUri += "&&Authorization=" + userId;
        } else {
            requestUri += "?Authorization=" + userId;
        }


        log.info("转发后{}",requestUri);
        response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
        response.sendRedirect("http://47.113.146.185:8888/" + requestUri);
        return false;
    }
}
